<?php defined('IN_LK') or die('Access Denied');
lk::lo_app('index_common', 'index', 0);
/**
 * 网站首页
 */
class index extends index_common
{
	private $content_db = null;
	
	public function __construct()
	{
		parent::__construct();
		$this->content_db = lk::load_model('content');
	}
	
	public function initialize()
	{
		$orderby = $where = '';
		$cid = isset($_GET['cid']) ? (int)$_GET['cid'] : 0;
		if(!in_array($cid, array(0, 1, 2)))
		{
			$cid = 0;
		}
		$obj = isset($_GET['o'])   ? (int)$_GET['o']   : 0;
		$px  = isset($_GET['px'])  ? (int)$_GET['px']  : 0;
		$p   = isset($_GET['p'])   ? (int)$_GET['p']   : 0;
		$lo  = isset($_GET['lo'])  ? (int)$_GET['lo']  : 0;
		if($cid){
			$where .= " AND `catid`='$cid'";
		}
		if($obj){
			$where .= " AND `objecttype`='$obj'";
		}
		if($px){
			$where .= " AND `pxfangshi`='$px'"; 
		}
		if($p){
			switch($p)
			{
				case 1:
					$where .= " AND `price`='0.00'";
					break;
				case 2:
					$where .= " AND `price`<='100.00' AND `price`>'0.00'";
					break;
				case 3:
					$where .= " AND `price`>'100.00' AND `price`<='500.00'";
					break;
				case 4:
					$where .= " AND `price`>'500.00' AND `price`<='1000.00'";
					break;
				case 5:
					$where .= " AND `price`>'1000.00'";
					break;
			}
		}
		if($lo)  //排序
		{
			switch($lo)
			{
				case 1:
					$orderby = "`feedback` DESC";
					break;
				case 2:
					$orderby = "`price` ASC";
					break;
				case 3:
					$orderby = '`inputtime` DESC';
					break;
			}
		}
		$s   = $this->content_list($where, $orderby);
		$hot_content = $this->content_db->get_hot(10);
		$jinghua     = $this->content_db->get_jinghua(10);
		$announ_content = $this->content_db->get_announ(10);
		include template('index');
	}
	
	/**
	 * 列表
	 * 使用缓存
	 */
	private function content_list($where, $order = '')
	{
		if(!$order)
		{
			$order = '`id` DESC';
		}
		$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
		/*$k    = md5(url());
		$kd   = 'l_' . $k;
		$kp   = 'l_p_' . $k;
		unset($k);
		//随机删除缓存 用户量大请适当调高此范围
		$pro  = mt_rand(1, 3);
		if($pro == 1)
		{
			delete_cache($kd);
			delete_cache($kp);
		}
		$data = get_cache($kd);
		$page_nav = get_cache($kp);
		$page_nav = $page_nav ? $page_nav[0] : '';
		if(!$data)
		{*/
			$data = $this->content_db->listinfo('*', '`status`=\'1\' AND `catid`!=\'3\'' . $where, $page, 20, $order);
			$page_nav = $this->content_db->page_nav;
			if(is_array($data) && $data)
			{
				foreach($data as $key => $list)
				{
					$data[$key]['updatetime'] = date('m-d H:i', $list['updatetime']);
					if($list['price'] == '0.00')
					{
						$data[$key]['price'] = '面议';
					}
					if(strpos($list['style'], ','))
					{
						$color = $fw = '';
						list($color, $fw) = explode(',', $list['style']);
						$data[$key]['styles'] = " style='color:" . $color . ";font-weight:" . $fw . "'";
					}
					else if($list['style'] == 'bold')
					{
						$data[$key]['styles'] = ' style="font-weight:bold"';
					}
					else if($list['style'])
					{
						$data[$key]['styles'] = ' style="color:' . $list['style'] . '"';
					}
					else
					{
						$data[$key]['styles'] = '';
					}
				}
			}
			unset($key, $list);
/*			set_cache($kd, $data);
			set_cache($kp, array(0=>$page_nav));
		}*/
		ob_start();
		include template('content_list');
		$s = ob_get_clean();
		return $s;
	}
}